﻿
(function () {
    appModule.controller('common.views.disputeInformationHandles.detail', [
         '$scope', '$uibModalInstance', 'abp.services.app.disputeInformation', 'disputeInformationId', 'abp.services.app.disputeType', 'disputeInformation', 'abp.services.app.area', 'abp.services.app.file',
        function ($scope, $uibModalInstance, disputeInformationService, disputeInformationId, disputeTypeService, disputeInformation, areaService, fileService) {
            var vm = this;
            vm.saving = false;
            //首先将disputeInformation数据设置为null
            vm.disputeInformation = null;


            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //初始化页面
            function init() {

                //绑定纠纷来源
                disputeInformationService.getAllDisSourceList().then(function (result) {
                    vm.disSourceList = result.data;
                });

                //绑定紧急程度
                disputeInformationService.getAllDisUrgencyList().then(function (result) {
                    vm.disUrgencyList = result.data;
                });

                //绑定纠纷分类
                disputeTypeService.getAllDisputeTypeList().then(function (result) {
                    vm.disputeTypeList = result.data.items;
                });

                //绑定镇控件
                areaService.getAreaByLevelAsync({ id: 0 }).then(function (result) {
                    vm.townList = result.data.items;
                });

                if (disputeInformationId != null) {

                    //绑定区控件
                    areaService.getAreaByLevelAsync({ id: disputeInformation.townID }).then(function (result) {
                        vm.areaList = result.data.items;
                    });

                    //绑定村控件
                    areaService.getAreaByLevelAsync({ id: disputeInformation.areaID }).then(function (result) {
                        vm.villageList = result.data.items;

                        //绑定数据
                        disputeInformationService.getDisputeInformationForEditAsync({
                            id: disputeInformationId
                        }).then(function (result) {
                            vm.disputeInformation = result.data.disputeInformation;

                            if (vm.disputeInformation.fileID != 0) {
                                //绑定文件数据
                                fileService.getFileForEditAsync({
                                    id: vm.disputeInformation.fileID
                                }).then(function (result) {
                                    vm.fileTitle = result.data.file.title;
                                    vm.fileUrl = result.data.file.url;
                                });
                            }

                            if (vm.disputeInformation.eventDescImgID != 0) {
                                //绑定文件数据
                                fileService.getFileForEditAsync({
                                    id: vm.disputeInformation.eventDescImgID
                                }).then(function (result) {
                                    vm.eventFileTitle = result.data.file.title;
                                    vm.eventFileUrl = result.data.file.url;
                                });
                            }
                        });

                    });

                }
                else {
                    disputeInformationService.getDisputeInformationForEditAsync({
                        id: disputeInformationId
                    }).then(function (result) {
                        vm.disputeInformation = result.data.disputeInformation;
                    });
                }

            }

            //绑定区
            vm.bindArea = function (townID) {
                areaService.getAreaByLevelAsync({ id: townID }).then(function (result) {
                    vm.areaList = result.data.items;
                });
            };

            //绑定村
            vm.bindVillage = function (areaID) {
                areaService.getAreaByLevelAsync({ id: areaID }).then(function (result) {
                    vm.villageList = result.data.items;
                });
            };

            //执行初始化方法
            init();
        }
    ]);
})();